package com.hcy.rabbitruntimeapi.task;

import com.hcy.rabbitbasicmodel.enums.Clients;
import com.hcy.rabbitruntimeapi.handler.MyWebSocketFrameHandler;
import com.hcy.rabbitruntimeapi.model.dto.WebSocketKey;
import io.netty.channel.Channel;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/3/29 0:28
 */
@Configuration
@EnableScheduling
public class ShowOnlineWorkers {

    Logger logger = LoggerFactory.getLogger(ShowOnlineWorkers.class);

    @Scheduled(fixedDelay = 10000)
    public void showWorkers(){

        ConcurrentHashMap<WebSocketKey, Channel> channels = MyWebSocketFrameHandler.channels;
        logger.debug("showWorkers = {}", ToStringBuilder.reflectionToString(channels));
        int workerCount = 0;
        int customerCount = 0;
        for (Map.Entry<WebSocketKey, Channel> channelEntry : channels.entrySet()) {
            WebSocketKey key = channelEntry.getKey();
            if(key.getClientId().equals(Clients.DRIVER.getName())){
                workerCount ++;
            }else if(key.getClientId().equals(Clients.CUSTOMER.getName())){
                customerCount ++;
            }
        }
        logger.info("showOnlineWorkers workerCount = [{}],customerCount = [{}]",workerCount,customerCount);
    }
}
